package leetcode.code413;

public class Solution extends Solution413 {

	@Override
	public int numberOfArithmeticSlices(int[] nums) {
		// 1,3,5,7,9,1,2,2,3,4,5,6,10,14,18,2,4,6,8,10
		if (nums.length < 3) {
			return 0;
		}
		int p1 = 0;
		int p2 = 1;
		int differ = nums[p2] - nums[p1];// 最近一个差值
		int ans = 0;// 累计结果
		while (p2 < nums.length) {
			// [2,1,3,4,2,3]
			if (p2 == nums.length - 1 || nums[p2 + 1] - nums[p2] != differ) {
				ans += this.count(p2 - p1 + 1);
				if (p2 < nums.length - 1) {
					differ = nums[p2 + 1] - nums[p2];
					p1 = p2;
				}
			}
			p2++;
		}
		return ans;
	}

	private int count(int size) {
		return size > 2 ? ((size - 1) * (size - 2)) >> 1 : 0;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
//		so.debug1();
//		so.debug2();
		so.debug3();
		so.debug4();

	}

}
